package com.controller;

import com.entity.Shopcar;
import com.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class ShopcarController {
    @Autowired
    CommodityService commodityService;
    /**
     * 添加商品进入购物车
     */
    @PutMapping("/user/center/addshopcar/{comid}")
    @ResponseBody
    public Integer addshopcar(@PathVariable("comid")String comid, HttpSession session){
        Shopcar shopcarn=new Shopcar();
        shopcarn.setAdtime(new Date()).setComid(comid).setUserid(session.getAttribute("userid").toString());
        Integer isTrue = commodityService.LookShopCarIsTrue(shopcarn);
        if(isTrue!=0){
            return 2;//已经存在了
        }
        commodityService.InsertShopIntoShopCar(shopcarn);
        return 1;
    }
    /**
     * 跳转到我的购物车
     * @return
     */
    @GetMapping("/user/shopcar")
    public String toshopcar(HttpSession session, ModelMap modelMap){
        modelMap.put("shopcount",commodityService.LookShopcarCount(session.getAttribute("userid").toString()));
        return "user/usershopcar";
    }
    /**
     * 查询我的购物车
     * @return
     */
    @GetMapping("/user/center/shopcar/{page}/{count}")
    @ResponseBody
    public List<Shopcar> myshopcar(HttpSession session, @PathVariable("page")Integer page, @PathVariable("count")Integer count){
        return commodityService.LookShopCarList(session.getAttribute("userid").toString(),page,count);
    }
    /**
     * 删除我的购物车
     * @return
     */
    @GetMapping("/user/center/delshopcar/{id}")
    @ResponseBody
    public String myshopcar(@PathVariable("id")Integer id){
        commodityService.DelMyShopCar(id);
        return "0";
    }


}


